package com.example.statistics.ui.purchase

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.Bank
import com.example.statistics.bean.SettlementFeeBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.*
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.httplibrary.model.HttpParams
import kotlinx.android.synthetic.main.activity_settlement_fee_edit.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.util.Date


/**
 * 结算单
 */
class SettlementFeeEditActivity : BaseActivity() {

    var accountId = 0

    override val title: String
        get() = "结算单"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settlement_fee_edit)
        var purchaseProductId = intent.getIntExtra(Constants.PURCHASE_PRODUCT_ID, -1)
        val data = intent.getSerializableExtra(Constants.DATA) as SettlementFeeBean?
        if (data != null) {
            purchaseProductId = data.purchaseProductId
            tv_date.contentText = data.date.goTimeFormat()
            tv_amount.contentText = data.amount.toString()
            tv_account.contentText = data.account
            tv_type.contentText = data.type
            accountId = data.accountId
        }
        tv_date.contentText = Date().format()
        var bankList: List<Bank>? = null

        httpGet<List<Bank>>(UrlConfig.BANK_ACCOUNT_LIST, config = {
            isShowDialog = true
            listParams = listOf()
        }) {
            bankList = it
        }

        tv_account.onContentClicked {
            singleDialog(bankList) {
                tv_account.contentText = it.itemName
                accountId = it.ID
            }
        }

        tv_type.onContentClicked {
            singleDialog("定金", "保证金", "尾款", "税款", "尾税", "出清") {
                tv_type.contentText = it
            }
        }

        topBarRightText("保存") {
            val params = mutableListOf(
                "purchaseProductId" to purchaseProductId,
                "containerNo" to "",
                "date" to tv_date.contentText,
                "amount" to tv_amount.contentText,
                "account" to tv_account.contentText,
                "accountId" to accountId,
                "type" to tv_type.contentText
            )
            val url = if (data == null) {
                UrlConfig.PURCHASE_SETTLEMENT_ADD
            } else {
                params.add("ID" to data.ID)
                params.add("bankFlowId" to data.bankFlowId)
                UrlConfig.PURCHASE_SETTLEMENT_UPDATE
            }
            httpPost<SettlementFeeBean>(url, config = {
                isShowDialog = true
                listParams = params
            }) {
                activityFinish(Constants.DATA to it)
            }
        }
    }
}